{extend name="../../base/view/common/base" /}
{block name="style"}
  <style>	
	.gougu-upload-files{background-color: #ffffff; border:1px solid #e4e7ed;color: #c0c4cc;cursor: not-allowed; padding:0 12px; width:180px; box-sizing: border-box; display: inline-block; font-size: inherit; height: 38px; line-height: 35px; margin-right:8px; border-radius:2px;}
	.gougu-upload-tips{color:#969696}
	.layui-form-item{margin-bottom:8px;}
	.layui-input-block{min-height:24px;}
  </style>
{/block}
<!-- 主体 -->
{block name="body"}
<div class="p-page">
	<form class="layui-form gg-form-bar border-x border-t">
		<div class="layui-input-inline" style="width:120px;">
			<select name="cate_id">
				<option value="">试题分类</option>
				{volist name=":get_base_data('question_cate')" id="v"}
				<option value="{$v.id}">{$v.title}</option>
				{/volist}
			</select>
		</div>
		<div class="layui-input-inline" style="width:100px;">
			<select name="types">
				<option value="">选择题型</option>
				{volist name=":get_question_types(1)" id="v"}
				<option value="{$v.id}">{$v.title}</option>
				{/volist}
			</select>
		</div>
		<div class="layui-input-inline" style="width:120px;">
			<select name="level">
				<option value="">选择难度</option>
				{volist name=":get_question_level(1)" id="v"}
					<option value="{$v.id}"}>{$v.title}</option>
				{/volist}
			</select>
		</div>
		<div class="layui-input-inline" style="width:300px;">
			<input type="text" name="keywords" placeholder="关键字" class="layui-input" autocomplete="off" />
		</div>
		<div class="layui-input-inline" style="width:150px">
			<button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="table-search"><i class="layui-icon layui-icon-search mr-1"></i>搜索</button>
			<button type="reset" class="layui-btn layui-btn-reset" lay-filter="table-reset">清空</button>
		</div>
	</form>
    <table class="layui-hide" id="test" lay-filter="test"></table>
</div>
<script type="text/html" id="toolbarDemo">
   <div class="layui-btn-group">
	<button class="layui-btn layui-btn-sm" lay-event="add">
		<span>+ 添加试题</span>
		<i class="layui-icon layui-icon-down layui-font-12"></i>
	</button>
	<button class="layui-btn layui-btn-normal layui-btn-sm" lay-event="import"><i class="layui-icon">&#xe62d;</i>批量导入</button>
  </div>
</script>
{/block}
<!-- /主体 -->

<!-- 脚本 -->
{block name="script"}
	<script>
	const question_types = {:json_encode(get_question_types(1),JSON_UNESCAPED_UNICODE)};
	const moduleInit = ['tool','tablePlus','uploadPlus'];
	function gouguInit() {
		var table = layui.tablePlus,tool = layui.tool,form = layui.form,dropdown = layui.dropdown,uploadPlus=layui.uploadPlus;
		layui.pageTable = table.render({
			elem: '#test'
			,toolbar: '#toolbarDemo'
			,title:'试题列表'
			,url: "/exam/question/datalist"
			,page: true
			,limit: 20
			,cellMinWidth: 60
			,height: 'full-114'
			,cols: [[
				{field:'id',width:80, title: 'ID号', align:'center'}
				,{field:'types_str',width:80, title: '试题类型', align:'center'}
				,{field:'cate',width:100, title: '所属分类', align:'center'}
				,{field:'title',title: '试题题干',minWidth:200,templet: function(d){
					var html='';
					if(d.is_important==1){
						html+='<span class="layui-badge">重点</span> ';
					}
					if(d.is_wrong==1){
						html+='<span class="layui-badge layui-bg-orange">易错</span> ';
					}
					html+=d.title;
					return html;
				}}
				,{field:'questionanswer',width:100, title: '答案', align:'center'}
				,{field:'level_str',width:80, title: '难易度', align:'center'}
				,{field:'score',width:60, title: '分数', align:'center'}
				,{field:'create_time',width:150, title: '创建时间', align:'center'}
				,{field:'status', title: '状态',width:60,align:'center',templet: function(d){
					var html1='<span class="green">正常</span>';
					var html2='<span class="yellow">下架</span>';
					if(d.status==1){
						return html1;
					}
					else{
						return html2;
					}
				}}
				,{width:156,fixed:'right', title: '操作', align:'center',templet: function(d){
					var html='';
					var btn='<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a><a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="view">详情</a>';
					var btn1='<a class="layui-btn layui-bg-orange layui-btn-xs" lay-event="disable">下架</a>';
					var btn2='<a class="layui-btn layui-btn-xs" lay-event="open">上架</a>';
					var btn3='<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>';
					if(d.status==1){
						html = '<div class="layui-btn-group">'+btn+btn1+btn3+'</div>';
					}
					else{
						html = '<div class="layui-btn-group">'+btn+btn2+btn3+'</div>';
					}
					return html;
				}}
			]]
		});
		
		table.on('tool(test)',function (obj) {
			if(obj.event === 'edit'){					
				tool.side("/exam/question/add?id="+obj.data.id);
			}
			if(obj.event === 'view'){					
				tool.side("/exam/question/view?id="+obj.data.id);
			}
			if(obj.event === 'disable'){
				layer.confirm('确定要下架该试题吗?', {icon: 3, title:'提示'}, function(index){
					let callback = function (e) {
						layer.msg(e.msg);
						if (e.code == 0) {
							layui.pageTable.reload();
						}
					}
					tool.post("/exam/question/set", { id: obj.data.id,status: 0}, callback);
					layer.close(index);
				});
			}
			if(obj.event === 'open'){
				layer.confirm('确定要上架该试题吗?', {icon: 3, title:'提示'}, function(index){
					let callback = function (e) {
						layer.msg(e.msg);
						if (e.code == 0) {
							layui.pageTable.reload();
						}
					}
					tool.post("/exam/question/set", { id: obj.data.id,status: 1}, callback);
					layer.close(index);
				});
			}
			if(obj.event === 'del'){
				layer.confirm('确定要删除该试题吗?', {icon: 3, title:'提示'}, function(index){
					let callback = function (e) {
						layer.msg(e.msg);
						if (e.code == 0) {
							layui.pageTable.reload();
						}
					}
					tool.delete("/exam/question/del", { id: obj.data.id}, callback);
					layer.close(index);
				});
			}
		});
		
		//表头工具栏事件
		table.on('toolbar(test)', function(obj){
			var checkStatus = table.checkStatus(obj.config.id); //获取选中行状态
			var data = checkStatus.data;
			var that = this;
			if (obj.event === 'add'){
				  dropdown.render({
					elem: that,
					show: true, // 外部事件触发即显示
					data: question_types,
					click: function(obj){
						tool.side("/exam/question/add?types="+obj.id);
					}
				  });
				return;
			}
			if (obj.event === 'import') {
				let importUpload = new uploadPlus({
					"title":'批量导入试题',
					"use":'import',
					"url":'/api/import/import_question',
					"import":{
						"template":'/static/home/file/试题导入模板.xlsx',
						"tips":'如果导入失败，请根据提示注意检查表格数据，所属分类、难易度需要是系统中存在的数据，如果不存在的话可能会导入失败。'
					},
					callback:function(res){
						layui.pageTable.reload();
					}
				});	
				return;				
			}
		});
	}
	</script>
{/block}
<!-- /脚本 -->